unit Unit48; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Menus, Unit49, Unit1, Unit22, DateUtils, Unit50, Unit51, LR_DBSet,
  LR_Class, Unit55;

type

  { TForm48 }

  TForm48 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow();
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem11Click(Sender: TObject);
    procedure MenuItem12Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
  private
    fecha_desde : string;
    fecha_hasta : string;
    funcionario : string;
    nombre_sucursal : string;
  public
    { public declarations }
  end; 

var
  Form48: TForm48; 

implementation

{ TForm48 }

procedure TForm48.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.SQL.Text := 'select rtc_cdg, rtc_fecha, rtc_hora, fnc_nombre, suc_nombre, '
    + 'rtc_descripcion, rtc_monto from vw_retiro_caja where momento >= ''' + fecha_desde
    + ''' and momento <= ''' + fecha_hasta + ''' and fnc_nombre ilike ''%' + funcionario
    + '%'' and suc_nombre ilike ''%' + nombre_sucursal + '%''';
    
  SQLQuery1.Open;
  
  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Fecha';
  DBGrid1.Columns[2].Title.Caption := 'Hora';
  DBGrid1.Columns[3].Title.Caption := 'Funcionario';
  DBGrid1.Columns[4].Title.Caption := 'Sucursal';
  DBGrid1.Columns[5].Title.Caption := 'Descripción';
  DBGrid1.Columns[6].Title.Caption := 'Monto';

  DBGrid1.Columns[0].Width := 60;
  DBGrid1.Columns[1].Width := 100;
  DBGrid1.Columns[2].Width := 100;
  DBGrid1.Columns[3].Width := 150;
  DBGrid1.Columns[4].Width := 105;
  DBGrid1.Columns[5].Width := 200;
  DBGrid1.Columns[6].Width := 65;
  
  TColumn(DBGrid1.Columns[6]).DisplayFormat := '$ ####0.00';
end;

procedure TForm48.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm48.FormShow();
begin
  fecha_desde := DateToStr(Date) + ' ' + '00:00';
  fecha_hasta := DateToStr(Date) + ' ' + '23:59';
  funcionario := '%';
  nombre_sucursal := Form1.mi_sucursal;
  
  ordenar_grid();
end;

procedure TForm48.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'desde' then
    ParValue := fecha_desde;
    
  if ParName = 'hasta' then
    ParValue := fecha_hasta;
end;

procedure TForm48.MenuItem10Click(Sender: TObject);
begin
  FormShow();
end;

procedure TForm48.MenuItem11Click(Sender: TObject);
begin
  frReport1.LoadFromFile('reporte_retiro_caja.lrf');
  frReport1.ShowReport;
end;

procedure TForm48.MenuItem12Click(Sender: TObject);
begin
  Form55.ShowModal;
  
  nombre_sucursal := Form55.nombre_sucursal;
  
  ordenar_grid;
end;

procedure TForm48.MenuItem4Click(Sender: TObject);
begin
  Form49.ShowModal;
  
  ordenar_grid();
end;

procedure TForm48.MenuItem5Click(Sender: TObject);
var
  codigo : string;
  QueryString : string;
  
begin
  codigo := InputBox('Cancelar Retiro', 'Ingrese el codigo de retiro a cancelar:', '');
  
  QueryString := 'delete from retiro_caja where rtc_cdg = ' + codigo;
  
  Form1.PQConnection1.ExecuteDirect('begin work;');
  Form1.PQConnection1.ExecuteDirect(QueryString);
  Form1.PQConnection1.ExecuteDirect('commit work;');

  ShowMessage('BAJA EXITOSA');
  
  ordenar_grid;
end;

procedure TForm48.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en el retiro de caja a modificar...');
end;

procedure TForm48.MenuItem8Click(Sender: TObject);
begin
  Form22.ShowModal;
  
  fecha_desde := Form22.fecha_desde;
  fecha_hasta := Form22.fecha_hasta;
  
  ordenar_grid();
end;

procedure TForm48.MenuItem9Click(Sender: TObject);
begin
  Form51.ShowModal;
  
  funcionario := Form51.funcionario;
  
  ordenar_grid();
end;

procedure TForm48.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
 if SQLQuery1.Active = True then
   SQLQuery1.Close;
end;

procedure TForm48.DBGrid1CellClick(Column: TColumn);
begin
  Form50.Edit3.Text := SQLQuery1.FieldByName('rtc_cdg').AsString;
  Form50.funcionario := SQLQuery1.FieldByName('fnc_nombre').AsString;
  Form50.Edit1.Text := SQLQuery1.FieldByName('rtc_descripcion').AsString;
  Form50.Edit2.Text := SQLQuery1.FieldByName('rtc_monto').AsString;

  Form50.ShowModal;
  
  ordenar_grid();
end;

initialization
  {$I unit48.lrs}

end.

